#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include "Func.h"


//有空节点的链表
//头节点不存放数据
pLink_List_t Single_Cycle_Link_List_Init() {

    pLink_List_t phead = (pLink_List_t)malloc(sizeof(Link_List_t));
    if (phead == NULL)
    {
        perror("malloc Single_Link_List fail");
        return phead;
    }
    memset(phead,0,sizeof(Link_List_t));
	phead->sex='0';
	phead->height=0;
	phead->score=0;
    phead->next = phead;

    return phead;
}

//创建节点
pLink_List_t Create_new_node(char *name,char sex,int height,float score){

    pLink_List_t pnew=(pLink_List_t)malloc(sizeof(Link_List_t));
    if(pnew==NULL)
    {
        perror("malloc a new node fail");
        return pnew;
    }
    
    strcpy(pnew->name,name);
    pnew->sex=sex;
    pnew->height=height;
    pnew->score=score;
    pnew->next=NULL;
    
    return pnew;
}

//从键盘创建学生节点
pLink_List_t Create_new_node_scanf(void){

    pLink_List_t pnew=(pLink_List_t)malloc(sizeof(Link_List_t));
    if(pnew==NULL)
    {
        perror("malloc a new node fail");
        return pnew;
    }
    char name[20];
	char sex;
	int height;
	float score;
    printf("请输入姓名：");
    scanf("%s",name);
    printf("请输入性别：");
    scanf("%s",&sex);
    printf("请输入身高：");
    scanf("%d",&height);
    printf("请输入成绩：");
    scanf("%f",&score);
    strcpy(pnew->name,name);
    pnew->sex=sex;
    pnew->height=height;
    pnew->score=score;
    pnew->next=NULL;
    return pnew;
}

#if 1
//头插法
void  Single_Cycle_Link_List_Head_Insert(pLink_List_t phead, pLink_List_t node)
{
    
   if(node == NULL)
   {
    perror("Head insert a node fail");
    return;
   }
   pLink_List_t plink=phead;
   //判断是否首节点
   if(phead->next==phead)
   {
    plink->next=node;
    node->next=phead;
    return;
   }
   //头插
   node->next=phead->next;
   phead->next=node;
}

#endif


#if 1
//查找学生
void Single_Link_List_Search(pLink_List_t phead)
{
    if(phead->next==NULL){
        printf("链表无节点");
        return;
    }
     char name[20];
    printf("请输入要查找的姓名：");
    scanf("%s",name);
    while(getchar()!='\n');
    printf("\n");

    pLink_List_t plink=phead->next;

    while(plink!=NULL)
    {
        if(strcmp(plink->name,name)==0)
        {
            printf("姓名%s,性别%c,身高%d,成绩%f",plink->name,plink->sex,plink->height,plink->score);
            return;
        }
        plink=plink->next;
    }

    if(plink==NULL)
    {
        printf("链表中找不到此数据\n");
        return;
    }

}
#endif

//删除学生信息
void Single_Cycle_Link_List_Random_Delet(pLink_List_t phead)
{
    if(phead->next==phead)
    {
        printf("无学生信息");
        return;
    }

    pLink_List_t plink=phead;
    pLink_List_t ptemp=NULL;
    char name[20];
    printf("请输入要删除的姓名：");
    scanf("%s",name);
    while(getchar()!='\n');
    printf("\n");
    //plink指向需要删除节点的前一个节点
    while(strcmp(plink->next->name,name)!=0)
    {
        if(plink->next==phead)
        {
            printf("无此学生\n");
            return;
        }

        plink=plink->next;
    }
    ptemp=plink->next;
    plink->next=ptemp->next;
    free(ptemp);
    ptemp=NULL;
}

//修改学生信息
void Single_Cycle_Link_List_Modify(pLink_List_t phead)
{
    char name[20],modifide_name[20];
    char sex;
    int page,choice,height,score;
    if (phead->next == phead)
    {
        printf("无学生信息");
        return;
    }

    printf("请输入要修改的学生姓名:");
    scanf("%s",name);
    while(getchar()!='\n');
 
    pLink_List_t plink = phead->next;

    while (plink != phead)
    {
        if (strcmp(plink->name,name)==0)
        {
            printf("选择需要修改的信息:1.姓名  2.性别  3.身高 4.分数  5.放弃修改\n");
            scanf("%d",&choice);

            while(choice!=5)
            {
                switch(choice)
                {
                    case 1:
                        printf("请输入新的学生姓名:");
                        scanf("%s",modifide_name);
                        while(getchar()!='\n');
                        strcpy(plink->name,modifide_name);
                        break;
                    case 2:
                        printf("请输入新性别:");
                        scanf("%s",&sex);
                        plink->sex=sex;
                        break;
                    case 3:
                        printf("请输入新身高:");
                        scanf("%d",&height);
                        plink->height=height;
                        break;
                    case 4:
                        printf("请输入新成绩:");
                        scanf("%d",&score);
                        plink->score=score;
                        break;
                    case 5:
                        return;
                        break;
                    default:
                        printf("输入错误，重新输入\n");
                        break;
                }
                printf("选择需要修改的信息:1.姓名  2.性别  3.身高 4.成绩  5.放弃修改\n");
                scanf("%d",&choice);
            }
            return;
        }
        plink = plink->next;
    }

    if (plink == phead)
    {
        printf("链表中找不到此数据\n");
        return;
    }


}


#if 1
//遍历链表
void Print_Link_List(pLink_List_t phead)
{
    pLink_List_t plink=phead->next;

    if(plink==phead)
    {
        perror("None Link List");
        return;
    }

    while(plink!=phead)
    {
        printf("姓名%s,性别%c,身高%d,成绩%f\n",plink->name,plink->sex,plink->height,plink->score);
        plink=plink->next;
    }
    printf("\n");

}
#endif

#if 1
// 释放链表
void Release_Cycle_Link_list(pLink_List_t phead)
{
    pLink_List_t plink = phead->next;
    pLink_List_t ptemp = NULL;

    while (plink != phead)
    {
        ptemp = plink->next;
        free(plink);
        plink = ptemp;
    }
    free(phead);
    printf("链表已经全部释放\n");
}
#endif